package com.ht.system.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import com.ht.common.core.convert.EntityDtoConverter;
import com.ht.common.core.domain.R;
import com.ht.common.security.annotation.InnerAuth;
import com.ht.system.api.dto.CustomsDto;
import com.ht.system.api.dto.EnterpriseDto;
import com.ht.system.api.dto.EnterpriseInfoDto;
import com.ht.system.domain.Customs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ht.common.core.convert.EntitySelectConverter;
import com.ht.common.core.domain.vo.EntitySelectVo;
import com.ht.common.core.utils.SnowflakeIdWorker;
import com.ht.common.core.web.controller.BaseController;
import com.ht.common.core.web.domain.AjaxResult;
import com.ht.common.core.web.page.TableDataInfo;
import com.ht.common.log.annotation.Log;
import com.ht.common.log.enums.BusinessType;
import com.ht.common.security.annotation.RequiresLogin;
import com.ht.common.security.annotation.RequiresPermissions;
import com.ht.common.security.utils.SecurityUtils;
import com.ht.system.domain.BaseFieldInfo;
import com.ht.system.domain.EnterpriseInfo;
import com.ht.system.domain.EnterpriseOper;
import com.ht.system.service.IEnterpriseInfoService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 企业备案Controller
 *
 * @author zmx
 * @date 2025-03-22
 */
@Api(tags = "企业备案接口")
@RestController
@RequestMapping("/enterpriseInfo")
public class EnterpriseInfoController extends BaseController {
    @Autowired
    private IEnterpriseInfoService enterpriseInfoService;

    /**
     * 查询企业备案列表
     */
    @ApiOperation("列表,权限:system:enterpriseInfo:list")
    @RequiresPermissions("system:enterpriseInfo:list")
    @GetMapping("/list")
    public TableDataInfo list(EnterpriseInfo enterpriseInfo) {
        startPage();
        List<EnterpriseInfo> list = enterpriseInfoService.selectEnterpriseInfoList(enterpriseInfo);
        return getDataTable(list);
    }

    @ApiOperation("企业下来查询")
    @RequiresLogin
    @GetMapping("/findList")
    public AjaxResult findList(EnterpriseInfo enterpriseInfo) {
        List<EnterpriseInfo> list = enterpriseInfoService.selectEnterpriseInfoList(enterpriseInfo);
        List<EntitySelectVo> result = EntitySelectConverter.convert(
                list,
                EnterpriseInfo::getEntCode,
                EnterpriseInfo::getEntName,
                EnterpriseInfo::getEntType
        );
        return AjaxResult.success(result);
    }


    /**
     * 获取企业备案详细信息
     */
    @ApiOperation("详细信息,权限:system:enterpriseInfo:list")
    @RequiresPermissions("system:enterpriseInfo:list")
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(enterpriseInfoService.selectEnterpriseInfoById(id));
    }

    /**
     * 新增企业备案
     */
    @ApiOperation("新增,权限:system:enterpriseInfo:add")
    @RequiresPermissions("system:enterpriseInfo:add")
    @Log(title = "企业备案", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody EnterpriseInfo enterpriseInfo) {
        enterpriseInfo.setCreateBy(SecurityUtils.getNickName());
        enterpriseInfo.setCreateTime(new Date());
        return toAjax(enterpriseInfoService.insertEnterpriseInfo(enterpriseInfo));
    }

    /**
     * 修改企业备案
     */
    @ApiOperation("修改,权限:system:enterpriseInfo:edit")
    @RequiresPermissions("system:enterpriseInfo:edit")
    @Log(title = "企业备案", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody EnterpriseInfo enterpriseInfo) {
        enterpriseInfo.setUpdateBy(SecurityUtils.getNickName());
        enterpriseInfo.setUpdateTime(new Date());
        return toAjax(enterpriseInfoService.updateEnterpriseInfo(enterpriseInfo));
    }

    /**
     * 删除企业备案
     */
    @ApiOperation("删除,权限:system:enterpriseInfo:remove")
    @RequiresPermissions("system:enterpriseInfo:remove")
    @Log(title = "企业备案", businessType = BusinessType.DELETE)
    @GetMapping("remove/{ids}")
    public AjaxResult remove(@PathVariable("ids") String[] ids) {
        return toAjax(enterpriseInfoService.deleteEnterpriseInfoByIds(ids));
    }


    /**
     * 新增企业操作员
     */
    @ApiOperation("新增企业操作员,权限:system:enterpriseOper:add")
    @RequiresPermissions("system:enterpriseOper:add")
    @Log(title = "企业备案", businessType = BusinessType.INSERT)
    @PostMapping("addOper")
    public AjaxResult addOper(@RequestBody EnterpriseOper enterpriseOper) {
        enterpriseOper.setCreateTime(new Date());
        enterpriseOper.setCreateBy(SecurityUtils.getNickName());
        enterpriseOper.setId(SnowflakeIdWorker.getUUID());
        return toAjax(enterpriseInfoService.addOpre(enterpriseOper));
    }


    @InnerAuth
    @GetMapping("findAllByEntCode/{entCode}")
    public R<EnterpriseInfoDto> findAllByEntCode(@PathVariable("entCode") String entCode) {
        EnterpriseInfo enterpriseInfo = enterpriseInfoService.selectAllByEntCode(entCode);
        return R.ok(EntityDtoConverter.convert(enterpriseInfo, EnterpriseInfoDto.class));
    }

    @InnerAuth
    @GetMapping("findByEntCode/{entCode}")
    public R<EnterpriseInfoDto> findByEntCode(@PathVariable("entCode") String entCode) {
        EnterpriseInfo enterpriseInfo = enterpriseInfoService.selectByEntCode(entCode);
        return R.ok(EntityDtoConverter.convert(enterpriseInfo, EnterpriseInfoDto.class));
    }

    @InnerAuth
    @GetMapping("findAllEnterprise")
    public R< List<EnterpriseDto>> findAllEnterprise() {
        List<EnterpriseInfo> list = enterpriseInfoService.findAllEnterprise();
        if (CollectionUtil.isNotEmpty(list)) {
            List<EnterpriseDto> dtos = list.stream().map(item -> {
                        EnterpriseDto dto =new EnterpriseDto();
                        dto.setEntCode(item.getEntCode());
                        dto.setEntName(item.getEntName());
                        return dto;
                    })
                    .collect(Collectors.toList());
            return R.ok(dtos);
        }
        List<EnterpriseDto> result = new ArrayList<EnterpriseDto>();
        return R.ok(result);
    }


    @GetMapping("getAllEnterprise")
    public AjaxResult getAllEnterprise() {
        List<EnterpriseInfo> list = enterpriseInfoService.findAllEnterprise();
        if (CollectionUtil.isNotEmpty(list)) {
            List<EnterpriseDto> dtos = list.stream().map(item -> {
                        EnterpriseDto dto =new EnterpriseDto();
                        dto.setEntCode(item.getEntCode());
                        dto.setEntName(item.getEntName());
                        return dto;
                    })
                    .collect(Collectors.toList());
            return AjaxResult.success(dtos);
        }
        List<EnterpriseDto> result = new ArrayList<EnterpriseDto>();
        return AjaxResult.success(result);
    }

}
